#include "quickcalculation.h"

QuickCalculation::QuickCalculation()
{

}

QuickCalculation::~QuickCalculation()
{

}


bool QuickCalculation::StringNameCheck(string const& sName)
{
    if(sName.size() > MAXNAMESIZE)
        return false;
    return true;
}

void QuickCalculation::GetName(string&  sName)
{
    cout << endl << "Insert name. MAX character input is 15 excluding enter: ";
    cin >> sName;

    while (!StringNameCheck(sName))
    {
        cout << endl << "Insert name. MAX character input is 15 excluding enter: " << endl;
        cin >> sName;
    }
}

/**
* \brief Input function for weight and lenght input.
* \brief The weight and lenght will be put in struct data.
* \brief Input that's lower then the minimum or higher then maximum will get and error message.
* \param data = "data" is a pointer to the struct data.
*/
void QuickCalculation::InputUserData(struct UserData&  data)
{
    data.iweight = 0; //Integer representing the input weight in kilograms. This is a variable in the struct data
    data.flenght = 0; //Float representing the input length in meters. "This is a variable in the struct data.
    string sTempChar; //Is a string of temporary charcters to check eccidental character input.

    while(data.iweight < MINWEIGHT_KG || data.iweight > MAXWEIGHT_KG)
    {

        cout << endl << "Insert the persons weight in kilograms from 40kg to 160kg: ";
        cin >> sTempChar;
        data.iweight = atoi(sTempChar.c_str()); //atoi fails when trying to convert an eccidental character input.

        if(data.iweight < MINWEIGHT_KG || data.iweight > MAXWEIGHT_KG)
        {
            cout << endl << "[ERROR: WEIGHT INPUT NOT ACCEPTED BECAUSE THE INPUT IS INCORRECT!]";
            cout << endl << "[PLEASE TRY AGAIN]." << endl;
        }
    }

    while(data.flenght < MINLENGHT_M || data.flenght >= MAXLENGHT_M)
    {
       cout << endl << "\nInsert the length in metres from 1.50m to 2.20m: ";
       cin >> sTempChar;
       data.flenght = atof(sTempChar.c_str()); //atof fails when trying to convert an eccidental character input.
       //"data" is a pointer to a struct and "lenght" is the lenght in the original struct

       if(data.flenght < MINLENGHT_M || data.flenght >= MAXLENGHT_M)
        {
            cout << endl << "[ERROR: LENGHT INPUT NOT ACCEPTED BECAUSE THE INPUT IS INCORRECT!]";
            cout << endl << "[PLEASE TRY AGAIN.]" << endl;
        }
    }
}

float QuickCalculation::QueteletIndexCalculation(struct UserData const& data)
{
    float fResult; //Functions as the result of the calculated quetelet index.
    fResult = data.iweight/pow(data.flenght,2); //The pow function is a standard math function that can be used by using the math.h library
    return fResult;
}

void QuickCalculation::ConvertQueteletIndexToString(float fIndex, string& sQueteletIndex)
{

    if(fIndex < UNDERWEIGHTMARGIN_QI)
    {
        sQueteletIndex = "Underweight";
    }
    else if (fIndex > OVERWEIGHTMARGIN_QI)
    {
        sQueteletIndex = "Overweight";
    }
    else
    {
        sQueteletIndex = "Healthy weight";
    }
}
